import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
const app = getApp();

Page({
    data: {
        show: true,
        username: "",
        windowSwitch: 1,
        key: "",
        alliance: '',
        controlNumber: false
    },

    onClickShow() {
        this.setData({ show: true });
    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },
    onShow() {
        const db = qq.cloud.database()
        const _ = db.command;
        app.data.show = false;
        app.data.whoNumber = "member";
        app.data.whoSign = "sign-in";
    },
    onUnload() {
        app.data.show = true;
    },
    onClickHide() {
        if (this.data.username != "") {
            this.setData({ show: false });
            const db = qq.cloud.database();
            db.collection(app.data.whoNumber).add({
                    data: {
                        Name: this.data.username,
                        unionId: app.data.unionId,
                        alliance: this.data.alliance
                    },
                })
                .then(res => {})
                .catch(err => {});
            db.collection(app.data.whoSign).add({
                data: {
                    userName: app.data.userName,
                    unionId: app.data.unionId,
                    alliance: app.data.alliance
                },
            })
            qq.switchTab({
                url: '../index/index'
            })
        }
    },

    getFromValue(ev) {
        this.setData({ username: ev.detail });
        app.data.userName = ev.detail;
    },

    async bindGetUserInfo(e) {
        const This = this;
        let key = false;
        const db = qq.cloud.database();
        const _ = db.command;
        await qq.login({
            success(res) {
                if (res.code) {
                    qq.cloud.callFunction({
                        name: 'GetUserInfo',
                        data: {
                            code: res.code
                        },
                    }).then(res => {
                        app.data.unionId = res.result.data.unionid;
                        db.collection(app.data.whoNumber)
                            .where({
                                unionId: _.eq(app.data.unionId)
                            })
                            .get()
                            .then(res => {
                                let memberAlliance;
                                let memberBool = true;
                                if (!res.data.length) {
                                    memberBool = false;
                                } else {
                                    memberAlliance = res.data[0].alliance;
                                }
                                db.collection('key').where({
                                    key: _.eq(This.data.key)
                                }).get().then((res) => {
                                    app.data.alliance = res.data[0].alliance;
                                    This.setData({ alliance: app.data.alliance });
                                    if (memberBool) {
                                        key = true;
                                        if ((res.data.length == 0) || (app.data.alliance != memberAlliance)) {
                                            key = false
                                        }
                                        if (!key || (app.data.alliance != memberAlliance)) {
                                            Toast('密钥错误');
                                            return;
                                        }
                                    } else {
                                        key = true;
                                        if (res.data.length == 0) {
                                            key = false
                                        }
                                        if (!key) {
                                            Toast('密钥错误');
                                            return;
                                        }
                                    }

                                    db.collection("black")
                                        .where({
                                            unionId: _.eq(app.data.unionId)
                                        })
                                        .get()
                                        .then(res => {
                                            if (res.data.length == 0) {
                                                db.collection(app.data.whoNumber)
                                                    .where({
                                                        unionId: _.eq(app.data.unionId)
                                                    })
                                                    .get()
                                                    .then(res => {
                                                        if (res.data.length != 0) {
                                                            app.data.userName = res.data[0].Name
                                                            qq.reLaunch({
                                                                url: '../index/index'
                                                            });
                                                        } else {
                                                            This.setData({ windowSwitch: 2 });
                                                        }
                                                    }).catch(err => {})
                                            } else {
                                                This.setData({ windowSwitch: 3 });
                                            }
                                        });
                                }).catch((err) => {
                                    console.log(err);
                                    Toast('密钥错误');
                                })
                            }).catch(err => {
                                console.log(err);
                            })

                    }).catch(err => {
                        Toast('进入失败');
                        console.log(err);
                    })
                } else {}
            }
        });
    },
    onChange(ev) {
        this.setData({ key: ev.detail });
    },
    isControlNumber() {
        this.setData({ controlNumber: !this.data.controlNumber });
        if (this.data.controlNumber) {
            Notify({ type: 'primary', message: '已切换成控号模式' });
            app.data.controlNumber = true;
            app.data.whoNumber = "controlMember";
            app.data.whoSign = "controlSign-in";
        } else {
            Notify({ type: 'primary', message: '已切换成自身模式' });
            app.data.controlNumber = true;
            app.data.whoNumber = "member";
            app.data.whoSign = "sign-in";
        }
    }
})